package com.onenine.app.fragment;

import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.onenine.app.R;
import com.onenine.app.activity.GameCreateActivity;
import com.onenine.app.activity.GameFilterActivity;
import com.onenine.app.db.DataManager;
import com.onenine.app.event.FloatGameEvent;
import com.onenine.app.event.GameFilterEvent;
import com.onenine.app.model.GameCategory;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.app.view.AttachDraggableImageView;
import com.onenine.app.view.dialog.InfoIncompleteDialog;
import com.onenine.base.base.BaseEventFragment;
import com.onenine.base.tab.SlidingTabLayout;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.common.util.ActivityUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class GameCenterFragment extends BaseEventFragment implements View.OnClickListener {

    public static GameFilterEvent sGameFilter;
    private ImageButton mBtnCreate, mBtnOption;
    private boolean isDataLoaded;
    private AttachDraggableImageView mDragGameImage;
    private SlidingTabLayout mCategoryTab;
    private ViewPager mCategoryPager;

    @Override
    protected int getLayout() {
        return R.layout.fragment_game_center_view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparentForImageViewInFragment(getActivity(), null);
        super.onCreate(savedInstanceState);
    }

    /**
     * 设置状态栏白字样式
     */
    private void setDarkModeStyle() {
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                StatusBarUtils.setDarkMode(getActivity());
                return false;
            }
        });
    }

    @Override
    protected void initView(View view) {
        mCategoryTab = view.findViewById(R.id.tl_tab);
        mCategoryPager = view.findViewById(R.id.vp_pager);
        mDragGameImage = view.findViewById(R.id.drag_game_view);
        mBtnOption = view.findViewById(R.id.btn_option);
        mBtnCreate = view.findViewById(R.id.btn_room_create);
        mCategoryTab.setIndicatorColor(RoleResLoader.getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR));
        view.findViewById(R.id.fl_top).setBackgroundResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.THEME_GRADIENT_BG));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            StatusBarUtils.setFullScreenLightMode(getActivity());
        } else {
            setDarkModeStyle();
            StatusBarUtils.setTransparentForImageViewInFragment(getActivity(), null);
            if (!isDataLoaded) {
                isDataLoaded = true;
                getGameCategoryData();
            }
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mBtnCreate.setOnClickListener(this);
        mBtnOption.setOnClickListener(this);
        mDragGameImage.setOnClickListener(this);
        mDragGameImage.setOnImageClickListener(this);
    }

    /**
     * 获取游戏分类数据
     */
    private void getGameCategoryData() {
        List<GameCategory> gameCategoryList = DataManager.getAllGameCategorys();
        if (gameCategoryList == null || gameCategoryList.isEmpty()) {
            CDataProcessor.syncGameCategory(new Runnable() {
                @Override
                public void run() {
                    showGameCategoryTab(DataManager.getAllGameCategorys());
                }
            });
        } else {
            showGameCategoryTab(gameCategoryList);
        }
    }

    /**
     * 显示分类标签
     *
     * @param categoryList
     */
    private void showGameCategoryTab(List<GameCategory> categoryList) {
        mCategoryPager.setAdapter(new GameCategoryFragmentAdapter(getChildFragmentManager(), categoryList));
        String[] titles = new String[categoryList.size()];
        for (int i = 0; i < categoryList.size(); i++) {
            titles[i] = categoryList.get(i).getGameName();
        }
        mCategoryTab.setViewPager(mCategoryPager, titles);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_room_create) {
            ActivityUtils.startActivity(getActivity(), GameCreateActivity.class);
        } else if (v.getId() == R.id.btn_option) {
            if (CHelper.isLabelInfoImPerfect()) {
                new InfoIncompleteDialog(getActivity()).show();
            } else {
                GameFilterActivity.setGameFilterEvent(sGameFilter);
                GameFilterActivity.launchActivity(getActivity());
            }
        } else if (v.getId() == R.id.drag_game_view) {
            FloatGameEvent event = (FloatGameEvent) v.getTag();
            if (event != null) {
                ActivityUtils.startActivity(getActivity()
                        , event.activityClazz);
            }
        }
    }

    private class GameCategoryFragmentAdapter extends FragmentStatePagerAdapter {

        private List<GameCategory> categoryList;

        public GameCategoryFragmentAdapter(FragmentManager fm, List<GameCategory> categoryLiost) {
            super(fm);
            this.categoryList = categoryLiost;
        }

        @Override
        public int getCount() {
            return categoryList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return GameCategoryFragment.newInstance(categoryList.get(position));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(FloatGameEvent event) {
        mDragGameImage.setTag(event);
        mDragGameImage.setVisibility(event.showFloat
                ? View.VISIBLE : View.GONE);
    }

}
